package com.bawei.wyg.mvvmcommon.mvvm.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.bawei.wyg.commonapplication.BaseApplication;
import com.bawei.wyg.mvvmcommon.mvvm.viewmodel.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

public abstract class BaseFragment<V extends ViewDataBinding,VM extends BaseViewModel> extends Fragment implements IView {

    protected V v;
    protected VM vm;
    protected View inflate;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //重复获取布局id出现id重复找这个位置的错
        inflate = inflater.inflate(bindLayout(), container, false);
         binding();
         getLifecycle().addObserver(vm);
        return inflate;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(vm.getUiChangeLiveData()!=null){
            unregisterUIChange();
        }
        getLifecycle().removeObserver(vm);
    }

    @Override
    public int bindLayout() {
        return 0;
    }

    @Override
    public void binding() {
        //如果出现问题及的查看这里
        v= DataBindingUtil.setContentView(getActivity(),bindLayout());
        v.setVariable(initVariable(),vm);
    }



    protected abstract int initVariable();

    @Override
    public void initViewModel() {
        ParameterizedType type= (ParameterizedType) getClass().getGenericSuperclass();
        Type[] types = type.getActualTypeArguments();
        vm=find(types[1].getClass());
    }
    private<VM extends ViewModel> VM find(Class cla){
        return (VM) new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(cla);
    }

    private void registerUIChange(){
        vm.getUiChangeLiveData().getShowDialog().observe(this,showDialogOB);
        vm.getUiChangeLiveData().getHideDialog().observe(this,hideDialogOB);
        vm.getUiChangeLiveData().getFinishActivity().observe(this,finishActivityOB);
        vm.getUiChangeLiveData().getStartActivity().observe(this,starActivityOB);
    }
    private void unregisterUIChange(){
        vm.getUiChangeLiveData().getShowDialog().removeObserver(showDialogOB);
        vm.getUiChangeLiveData().getShowDialog().removeObserver(hideDialogOB);
        vm.getUiChangeLiveData().getShowDialog().removeObserver(finishActivityOB);
        vm.getUiChangeLiveData().getShowDialog().removeObserver(starActivityOB);

    }

    //定义四个相关方法的观察者
    Observer<String> showDialogOB=new Observer<String>() {
        @Override
        public void onChanged(String s) {
            showDialog();
        }
    };

    Observer<String> hideDialogOB=new Observer<String>() {
        @Override
        public void onChanged(String s) {
            hideDialog();
        }
    };

    Observer<String> finishActivityOB=new Observer<String>() {
        @Override
        public void onChanged(String s) {
            finishActivity();
        }
    };

    Observer<Map<String,Object>> starActivityOB=new Observer<Map<String, Object>>() {
        @Override
        public void onChanged(Map<String, Object> map) {
            starActivity(map);
        }
    };


    private void showDialog(){

    }
    private void hideDialog(){

    }

    private void finishActivity(){

    }

    private void starActivity(Map<String,Object> map){

    }
}
